/**
 * 		UserBean.java
 *  Adrián E. Córdoba 		04/05/2014
 */
package ar.com.adriancordoba.app.enterprise.rosc.web.beans;

import javax.faces.application.NavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpSession;

import org.jboss.logging.Logger;

import ar.com.adriancordoba.app.enterprise.rosc.ejb.stateless.interfaces.AuthenticatorRemote;
import ar.com.adriancordoba.app.enterprise.rosc.model.entities.User;

/**
 * 
 * @author Adrián E. Córdoba
 */
@ManagedBean
@SessionScoped
public class UserBean {
	private String username;
	private String password;

	private static Logger logger = Logger.getLogger(UserBean.class);

	/**
	 * 
	 */
	public UserBean() {
	}

	/**
	 * 
	 * @return
	 */
	public Object login() {
		Navigation result = null;
		InitialContext initialContext = null;
		AuthenticatorRemote authenticator = null;

		logger.debug("Username " + username + " login attempt.");

		try {
			initialContext = new InitialContext();
			authenticator = (AuthenticatorRemote) initialContext.lookup("java:global/RetailOfficeServicesCenter-ear/RetailOfficeServicesCenter-ejb/Authenticator!ar.com.adriancordoba.app.enterprise.rosc.ejb.stateless.interfaces.AuthenticatorRemote");
		} catch (NamingException e) {
			logger.error("Cannot get 'Authenticator' object.");
		}
		if (authenticator != null) {
			User user = authenticator.authenticate(username, password);
			if (user != null) {
				FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("user", user);
				result = Navigation.SUCCESS;
				logger.debug(username + " is logged in the system.");
			} else {
				result = Navigation.FAILURE;
				logger.debug(username + " cannot logged in the system.");
			}
		}

		return result;
	}

	/**
	 * 
	 * @return
	 */
	public Object logout(){
		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.invalidate();
		return Navigation.FAILURE;
	}
	
	/**
	 * 
	 * @param event
	 */
	public void checkLogin(ComponentSystemEvent event){
		FacesContext context = FacesContext.getCurrentInstance();
		NavigationHandler navigationHandler = context.getApplication().getNavigationHandler();
		User user = (User) context.getExternalContext().getSessionMap().get("user");
		if(user == null)
			navigationHandler.handleNavigation(context, null, Navigation.FAILURE.toString());
	}
	
	/**
	 * Enumerates the navigation possibilities.
	 * @author Adrián E. Córdoba
	 */
	public enum Navigation {
		SUCCESS, FAILURE
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}
}
